"use strict";
require([
    './dev/layer',
    'polyfill',
    './dev/nav',
    './dev/menu',
    './dev/pagenations',
    'text!tpl/staff.hbs',
    './conf/baseConfig',
    './dev/helper',
    './dev/http'
], function (layer, polyfill, nav, menu, Pager, staff, baseConfig, helper, http) {
    //layer.loadLayer();
    nav.showNav();
    //menu.showMenu();



    (function (b) {
        b.fn.extend({
            "mouseMove": function (c) {
                const that = b(this);
                that.hover(function (k) {
                    const u = b(this).find(c);
                    const p = k.clientX;
                    const n = k.clientY;
                    const s = parseInt(b(this).offset().top);
                    const o = parseInt(s + b(this).height());
                    const g = parseInt(b(this).offset().left);
                    const d = parseInt(g + b(this).width());
                    const m = Math.abs(n - s);
                    const j = Math.abs(n - o);
                    const i = Math.abs(p - g);
                    const f = Math.abs(p - d);
                    const h = Math.min(m, j, i, f);
                    const q = k.type;
                    switch (h) {
                        case m:
                            a("top", q, u);
                            break;
                        case j:
                            a("bottom", q, u);
                            break;
                        case i:
                            a("left", q, u);
                            break;
                        case f:
                            a("right", q, u)
                    }
                })
            }
        });

        function a(c, d, f) {
            const e = 300;
            if (c == "top") {
                if (d == "mouseenter") {
                    b(b(f)[0]).stop(true, true);
                    b(b(f)[0]).css({
                        display: "block",
                        top: "-100%",
                        left: "0"
                    }).animate({
                        top: 0,
                        left: 0
                    }, e)
                } else {
                    b(b(f)[0]).animate({
                        display: "block",
                        top: "-100%",
                        left: "0"
                    }, e)
                }
            }
            if (c == "left") {
                if (d == "mouseenter") {
                    b(b(f)[0]).stop(true, true);
                    b(b(f)[0]).css({
                        display: "block",
                        top: "0",
                        left: "-100%"
                    }).animate({
                        left: 0,
                        top: 0
                    }, e)
                } else {
                    b(b(f)[0]).animate({
                        display: "block",
                        left: "-100%"
                    }, e)
                }
            }
            if (c == "bottom") {
                if (d == "mouseenter") {
                    b(b(f)[0]).stop(true, true);
                    b(b(f)[0]).css({
                        display: "block",
                        top: "100%",
                        left: "0"
                    }).animate({
                        top: 0,
                        left: 0
                    }, e)
                } else {
                    b(b(f)[0]).animate({
                        display: "block",
                        top: "100%",
                        left: "0"
                    }, e)
                }
            }
            if (c == "right") {
                if (d == "mouseenter") {
                    b(b(f)[0]).stop(true, true);
                    b(b(f)[0]).css({
                        display: "block",
                        top: 0,
                        left: "100%"
                    }).animate({
                        left: "0%",
                        top: 0
                    }, e)
                } else {
                    b(b(f)[0]).animate({
                        display: "block",
                        left: "100%"
                    }, e)
                }
            }
        }
    }
    )($);

    const add0 = (m)=>{
        return m<10?'0'+m:m
    }

    const format = (time) =>{
        //shijianchuo是整数，否则要parseInt转换
        var time = new Date(time);
        var y = time.getFullYear();
        var m = time.getMonth()+1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
    }

    const students ={};

    const getUserList = function () {
        http.commGet(`${baseConfig.serUrl}/a/findUserList`, "", (data) => {

            //console.log(data)
            students.list =[];

            $.each(data, (index, item) => {
              const obj = {
                    id: item.id,
                    imgSrc:baseConfig.serUrl+item.picUrl,
                    title:item.userName,
                    depa:item.depa,
                    position:item.position?item.position:'工作人员',
                    age:item.age?tem.age:'暂无数据',
                    entryTime:format(item.createTime)
                };
                students.list.push(obj)
            });

            Pager.pageRender(students, "#pagination-container", "#data-container");

        }, (err) => {
            console.log(err);
        }, '')
    };

    getUserList();

    const randomFilenam = function () {
      let str ='';
      const chars = ["a", "b" ,"c","d","e","f","g","h","i","j","k", "l", "m", "n"];
      for (let i=0;i<5;i++){
        const rand = Math.round(Math.random()*10);
        str += chars[rand]
      }
      return str
    };

    $(document).on("click", ".grid-content" , function(){
        const getId = $($(this).children()[0]).data("id");
        const index = helper.findIndexData(getId, students.list, "id")
        sessionStorage.setItem("creater", helper.trimTool(students.list[index].title))
        layer.setlayTpl("laytpl", "std", staff, students.list[index])
        getBook(index).then((data) =>{
              const confTab = {
                elem: "#test0",
                cellMinWidth: 80,
                size: "sm",
                even: true,
                page: true,
                limit: 11,
                cols: [[
                  { field: 'id', title: '序号', sort: true }
                  , { field: 'username', title: '文章名称', }
                  , { field: 'email', title: '发表时间', sort: true }
                  ,{ field: 'key', title: '文章编号', sort: true }
                ]],
                data: data,
                skin: 'line'
              };

              const confPopup = {
                type: 1
                , title: false //不显示标题栏
                , closeBtn: true
                , area: '1000px;'
                , shade: 0.8
                , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                , btn: ["<i class='fa fa-plus fa-lg fat-bg'></i>&nbsp;&nbsp;发布文章"]
                , btnAlign: 'c'
                , moveType: 1 //拖拽模式，0或者1
                , content: $('#std')
                , success: function (layero) {

                  sessionStorage.setItem("filename",randomFilenam())
                  const btn = layero.find('.layui-layer-btn');
                  btn.find('.layui-layer-btn0').attr({
                    href: 'textEdit.html'
                  });
                }
              };

              layer.showStaff(confPopup, confTab, data, helper.findIndexData)

        })
    });

    const getBook = function (index) {
      const   dtd = $.Deferred(),
              params = {
                  id: index+1
              };
        http.commPost(`${baseConfig.serUrl}/a/findArticleByUserId`, params, (data) => {
            const list =[];
            $.each(data, (index, item) =>{
              const obj = {
                id: index+1,
                username: item.title,
                email: item.createTime,
                key:item.id,
                book:item.listUrl
              };
              list.push(obj)
            });
            dtd.resolve(list);
        }, (err) => {
            console.log(err);
           dtd.reject();
        }, '');


        return dtd.promise();
    };




});
